package com.example.home_app.base

import  android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import com.android.frame.mvp.CommonBaseFragment
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.animation.BaseAnimation
import com.example.home_app.R
import com.example.home_app.adapter.AnimationAdapter
import com.example.home_app.animator.CustomAnimation1
import com.example.home_app.animator.CustomAnimation2
import com.example.home_app.databinding.FragmentFrameBinding
import com.example.home_app.util.Status
import com.gyf.immersionbar.BarHide
import com.gyf.immersionbar.ImmersionBar


/**
 * Created by xuzhb on 2019/9/7
 * Desc:框架篇
 */
class FrameFragment : CommonBaseFragment<FragmentFrameBinding>() {
    private var mAnimationAdapter: AnimationAdapter? = null


    companion object {
        fun newInstance() = FrameFragment()
    }
    override fun handleView(savedInstanceState: Bundle?) {
        ImmersionBar.with(this).titleBar(R.id.toolbar).init()
        //隐藏状态栏
        ImmersionBar.with(this).hideBar(BarHide.FLAG_HIDE_STATUS_BAR).init()
        ImmersionBar.with(this).titleBar(R.id.toolbar).keyboardEnable(true).init()
        mRecyclerView?.setHasFixedSize(true)
        mRecyclerView?.layoutManager = LinearLayoutManager(context)
        initAdapter()
        initMenu()
    }

    override fun initListener() {

    }

    private fun initAdapter() {

        mAnimationAdapter = AnimationAdapter()
        //打开动画
        mAnimationAdapter?.animationEnable ?: true
        mAnimationAdapter!!.addChildClickViewIds(R.id.img, R.id.tweetName, R.id.tweetText)
        mAnimationAdapter!!.setOnItemChildClickListener { adapter, view, position ->

            var status = adapter.getItem(position) as Status
            when (view.id) {
                R.id.img ->  "img:" + status.getUserName()
                R.id.tweetName ->  "name:" + status.getUserName()
                R.id.tweetText -> "tweetText:" + status.getUserName()
                else -> {
                }
            }
        }
        mAnimationAdapter?.adapterAnimation ?: CustomAnimation2()
        mRecyclerView?.adapter = mAnimationAdapter
    }

    private fun initMenu() {
        binding.spinner!!.setItems(
            "AlphaIn",
            "ScaleIn",
            "SlideInBottom",
            "SlideInLeft",
            "SlideInRight",
            "Custom1",
            "Custom2"
        )
        binding.spinner!!.setOnItemSelectedListener { view, position, id, item ->
            when (position) {
                0 -> mAnimationAdapter!!.setAnimationWithDefault(BaseQuickAdapter.AnimationType.AlphaIn)
                1 -> mAnimationAdapter!!.setAnimationWithDefault(BaseQuickAdapter.AnimationType.ScaleIn)
                2 -> mAnimationAdapter!!.setAnimationWithDefault(BaseQuickAdapter.AnimationType.SlideInBottom)
                3 -> mAnimationAdapter!!.setAnimationWithDefault(BaseQuickAdapter.AnimationType.SlideInLeft)
                4 -> mAnimationAdapter!!.setAnimationWithDefault(BaseQuickAdapter.AnimationType.SlideInRight)
                5 -> mAnimationAdapter?.adapterAnimation ?: CustomAnimation1()
                6 -> mAnimationAdapter?.adapterAnimation ?: CustomAnimation2()
                else -> {
                }
            }

            mRecyclerView!!.adapter = mAnimationAdapter
        }
        //init firstOnly state
        mAnimationAdapter?.isAnimationFirstOnly ?: false
        binding.switchButton!!.setOnCheckedChangeListener { buttonView, isChecked ->
            if (isChecked) {
                mAnimationAdapter?.animationEnable ?: true
            } else {
                mAnimationAdapter?.animationEnable ?: false
            }
            mAnimationAdapter!!.notifyDataSetChanged()
        }
    }


}


